package com.tangula.web.kashine.business;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tangula.web.kashine.entity.AccountEntity;
import com.tangula.web.kashine.entity.LoginForm;
import com.tangula.web.kashine.entity.UpLoadImageForm;
import com.tangula.web.kashine.repository.AccountEntityRepository;

@Service
public class LoginBusinessImpl implements LoginBusiness{
	@Autowired
	AccountEntityRepository accountRepository;
	public AccountEntity signup(LoginForm account) {
		if(account!=null) {
			AccountEntity acc = accountRepository.findByEmail(account.getUsername());
			if(acc!=null&&acc.getPassword().equals(account.getPassword())) {
				return acc;
			}else {
				return null;
			}
		}else {
			return null;
		}
	}
	@Override
	public AccountEntity register(AccountEntity account) {
		if(account!=null&&accountRepository.findByEmail(account.getEmail())==null) {
				account.setBlock("y");
				account.setMoney(0);
				return accountRepository.saveAndFlush(account);
				
		}else {
			return null;
		} 

	}
	@Override
	public AccountEntity loadAccount(String email) {
		return accountRepository.findByEmail(email);
	}
	@Override
	public AccountEntity upLoadAccountImage(UpLoadImageForm image) {
		if(image!=null) {
			AccountEntity account = accountRepository.findById(image.getAccountId()).orElse(null);
			account.setImage(image.getImageId());
			return accountRepository.saveAndFlush(account);
		}else {
			return null;
		}
	}

}
